//
//  SWInvitationHandleViewController.swift
//  ChildStory
//  邀请处理页面（接受、拒绝）
//  Created by linhan on 16/9/5.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWInvitationHandleViewController: SWBaseViewController,SWAPIComponent
{
    //强制完成创建任务（不能返回）
    var forceSupplement:Bool = false
    
    private var _invitation:SWInvitation
    private var _imageView:UIImageView = UIImageView()
    
    init(invitation:SWInvitation)
    {
        _invitation = invitation
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    override func viewDidAppear(_ animated: Bool)
    {
        super.viewDidAppear(animated)
        if appearedFirstTime
        {
            appearedFirstTime = false
            if forceSupplement
            {
                //当到资料补全页来，说明已经注册成功，之前的注册登录页面已经不再需要了
                navigationController?.viewControllers = [self]
            }
        }
    }
    
    private func setup()
    {
        var inviter:String = ""
        if let screenName = _invitation.screenName , screenName != ""
        {
            inviter = screenName
        }
        else if let babiesNames = _invitation.babiesNames
        {
            inviter = babiesNames.joined(separator: "、")
            if let roleName = _invitation.roleName{
                inviter += "的\(roleName)"
            }
        }
        
        title = "\(inviter)邀请你加入TA的小屋"
        view.backgroundColor = UIColor.white
        
        _imageView.frame = CGRectMake(0, 90, view.width, view.width)
        _imageView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        view.addSubview(_imageView)
        
        let provisionLabel = UILabel()
        provisionLabel.frame = CGRectMake(0, _imageView.bottom + 30, view.width, 40)
        provisionLabel.numberOfLines = 0
        provisionLabel.textAlignment = .center
        provisionLabel.text = "加入对方小屋后，你将获得:\n\n 由你们共同管理的新的咿呀小屋"
        provisionLabel.textColor = UIColor.darkGray
        view.addSubview(provisionLabel)
        
        
        let acceptBtn = UIButton(type:.system)
        acceptBtn.setTitle("接受", for: .normal)
        acceptBtn.sizeToFit()
        acceptBtn.frame = CGRectMake(0, provisionLabel.bottom + 30, acceptBtn.width, acceptBtn.height)
        acceptBtn.addTarget(self, action: #selector(acceptBtnTapped), for: .touchUpInside)
        view.addSubview(acceptBtn)
        
        let denyBtn = UIButton(type:.system)
        denyBtn.setTitle("拒绝", for: .normal)
        denyBtn.sizeToFit()
        denyBtn.frame = CGRectMake(0, provisionLabel.bottom + 30, denyBtn.width, denyBtn.height)
        denyBtn.addTarget(self, action: #selector(denyBtnTapped), for: .touchUpInside)
        view.addSubview(denyBtn)
        
        LayoutUtil.distributeHAverageABS([acceptBtn, denyBtn], containerWidth: view.width)
    }
    
    //处理邀请
    private func handleInvitation(_ grant:Bool)
    {
        let req = SWInvitationHandleReq()
        req.grant = grant
        req.invitationID = _invitation.invitationID
        req.successCallback = {[weak self] response,cache in
            self?.acceptSuccess()
        }
        req.failCallback = {[weak self] error in
            if error.code == 3026{
                self?.acceptSuccess()
            }else{
                SVProgressHUD.showError(withStatus: "接受邀请失败，请重试")
            }
        }
        load(req)
    }
    
    private func acceptSuccess()
    {
        if forceSupplement
        {
            SWMainApplication.sharedApplication.rootViewController?.showMainContent()
        }
        else
        {
            SVProgressHUD.showSuccess(withStatus: "成功接受邀请")
        }
    }
    
    @objc private func acceptBtnTapped()
    {
        handleInvitation(true)
    }
    
    @objc private func denyBtnTapped()
    {
        handleInvitation(false)
    }
    
    
    
}
